package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.constant.MessageConstant;
import com.dao.OrderSettingDao;
import com.entity.Result;
import com.pojo.OrderSetting;
import com.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;

@Service(interfaceClass = com.service.OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    //上传文件
    @Override
    public Result upload(List<String[]> list) throws IOException {
        Result result = null;
        try {
            List<OrderSetting> orderlist = new ArrayList<>();
            if (list != null && list.size() > 0) {
                //开始遍历每一行数据,并取出,存储到集合中
                for (String[] row : list) {
                    OrderSetting or = new OrderSetting(new Date(row[0]), Integer.parseInt(row[1]));
                    orderlist.add(or);
                }
                //开始调用Dao层往数据库存储数据
                int i = 0;
                if (orderlist != null && orderlist.size() > 0) {
                    //根据日期查询下看是否存在相同日期
                    for (OrderSetting os : orderlist) {
                        Date orderDate = os.getOrderDate();
                        int count = orderSettingDao.findByOrderDate(orderDate);
                        if (count != 0) {
                            //说明已经存在该日期,那么就修改即可
                            orderSettingDao.updateNumberByOrderDate(orderDate, os.getNumber());
                        } else {
                            //说明该日期还不存在,添加即可
                            orderSettingDao.add(os);
                        }
                    }
                    i = 1;
                }
                if (i != 0) {
                    //说明插入成功
                    result = new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
                } else {
                    result = new Result(false, MessageConstant.ORDERSETTING_FAIL);
                }

            } else {
                return new Result(false, "文件内部没有数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, "服务器异常了,请联系管理员");
            throw e;
        } finally {
            //返回结果
            return result;
        }

    }

    //测试代码
    @Override
    public void test() {
        System.out.println("哈哈,你来啦");
    }

    //查询用户传递的月份,对应的相关数据
    public Result findByYearAndMonth(String date) {
        String d="%"+date+"%";
        List<OrderSetting> list = orderSettingDao.findByYearAndMonth(d);
        //转换数据格式,传递给前台
        List<Map> datas=new ArrayList<>();
        for (OrderSetting o : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("reservations", o.getReservations());
            map.put("number", o.getNumber());
            map.put("date",o.getOrderDate().getDate());
            datas.add(map);
        }
        return new Result(true,"成功了",datas);
    }

    //设置,单天的数据,原则是存在就修改,不存在就新增
    public Result edit(OrderSetting orderSetting) {
        Date date = orderSetting.getOrderDate();
        if (date==null){
            return new Result(false,"无具体日期");
        }
        int number = orderSetting.getNumber();
        //先查询是否存在
        int count = orderSettingDao.findByOrderDate(date);
        if (count!=0){
            //说明存在,则修改
            int i = orderSettingDao.updateNumberByOrderDate(date, number);
            System.out.println(i);
        }else {
            //不存在,则新增
            orderSettingDao.add(orderSetting);
        }
        return new Result(true,"设置预约人数成功");
    }

}
